package com.fbty.mt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.fbty.base.dao.MtJdbcDao;
import com.fbty.base.domain.Page;
import com.fbty.mt.dao.RecordDao;
import com.fbty.mt.dao.UserDao;
import com.fbty.mt.domain.Record;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.ManagerService;
/**
 * 会员务类
 * @author thinkpad
 *
 */
public class ManagerServiceImpl implements ManagerService {
	private MtJdbcDao mtJdbcDao;
	private UserDao userDao;
	private RecordDao recordDao;
	/**
	 * 分页显示共享类ip
	 * @param pageNo
	 * @param flag
	 * @param parms
	 * @return
	 */
	public Page pageIpInsos(Integer pageNo,Integer pageSize,String parms) {
		String sql =  " select * from tab_ip_info tp order by id desc";
		List list = mtJdbcDao.getJdbcTemplate().queryForList(sql);
		Integer count = list.size();
		return new Page(list, pageNo*pageSize, count, pageSize);
	}
	/**
	 * 查看共享类购物车
	 * @param pids
	 * @return
	 */
	@Override
	public Page selectBuy(Set set,Integer pageNo,Integer pageSize) {
		Iterator iterator = set.iterator();
		StringBuilder sb = new StringBuilder();
		while (iterator.hasNext()) {
			String str = iterator.next()+"";
			if(!"".equals(str)) {
				sb.append(str+",");
			}
			
		}
		sb.append("0");
		String sql =  " select * from tab_ip_info tp where id in("+sb.toString()+") order by id desc";
		List list = mtJdbcDao.getJdbcTemplate().queryForList(sql);
		Integer count = list.size();
		return new Page(list, pageNo*pageSize, count, pageSize);
	}
	/**
	 * 购买共享类IP
	 * @param pids
	 * @return
	 */
	@Override
	public String updateBuy(Set set,User user,Integer num,HttpSession session) {
		int len = set.size();
		Iterator iterator = set.iterator();
		StringBuilder sb = new StringBuilder();
		String message = "";
		while (iterator.hasNext()) {
			sb.append(iterator.next()+",");
		}
		sb.append("0");
		String sql = " select pid from tab_record where uid="+user.getId()+" and pid in ("+sb.toString()+")";
		List<Map> list = mtJdbcDao.getJdbcTemplate().queryForList(sql);
		List<Record> list2 = new ArrayList<Record>(); //保存去重后得到的记录
		if(list.size() == 0) { //说明购买的ip里没有重复的
			Iterator i = set.iterator();
			while (i.hasNext()) {
				Record ipRecord = new Record();
				ipRecord.setBuyDate(new Date());
				ipRecord.setUid(user.getId());
				ipRecord.setState(2);
				ipRecord.setPid(Integer.parseInt(i.next().toString()));
				list2.add(ipRecord);
			}
			recordDao.saveAll(list2);
			message = "<span style='color:red;font-size:15px'>购买成功,您一共购买了"+list2.size()+"条!</span>";
		} else { //说明购买的ip里面有重复的，去重，然后进行保存，在减去用户的数量
			for(Map map : list) {
				set.remove(map.get("pid")+"");//将重复的id从set里面去掉
			}
			Iterator it= set.iterator();
			while(it.hasNext()) {
				Record ipRecord = new Record();
				ipRecord.setBuyDate(new Date());
				ipRecord.setUid(user.getId());
				ipRecord.setState(2);
				ipRecord.setPid(Integer.parseInt(it.next()+""));
				list2.add(ipRecord);
			}
			recordDao.saveAll(list2);
			message = "<span style='color:red;font-size:15px'>您预计购买"+len+"条,其中有"+list.size()+"条重复,总计购买："+list2.size()+"条!</span>";
		}
		User user2 = userDao.get(user.getId());
		user2.setNum(user2.getNum()-list2.size());
		userDao.update(user2);
		session.setAttribute("info", "<span style='color:green'>"+user.getUsername()+"</span> | 普通会员 | 您目前还剩下<span style='color:red'>"+user2.getNum()+"</span>条购买数量 | ");
		System.out.println(user2.getNum());
		return message;
	}

	/**
	 * 显示价格
	 * @param pids
	 * @return
	 */
	@Override
	public List showPrice(String roleId) {
		String sql = "";
		if("".equals(roleId)||null == roleId) {
			sql = " select * from tab_price order by sort ";
		} else {
			sql = " select * from tab_price where type="+roleId+" order by price ";
		}
		List<Map> list = mtJdbcDao.getJdbcTemplate().queryForList(sql);
		List<Map> list2 = new ArrayList<Map>();
		for(int i=0; i<list.size(); i++) {
			Map map = new HashMap();
			map.put("id", list.get(i).get("id"));
			map.put("num", list.get(i).get("num"));
			map.put("price", list.get(i).get("price"));
			if("2".equals(roleId)) {
				map.put("time", list.get(i).get("time")+"小时");
			} else {
				map.put("time", list.get(i).get("time")+"天");
			}
			map.put("roleId", roleId);
			list2.add(map);
		}
		return list2;
	}
	/**
	 * 管理员设定价格
	 * @param pids
	 * @return
	 */
	@Override
	public List<Map<String, String>> price(String params) {
		String sql = " select * from tab_price ";
		return mtJdbcDao.getJdbcTemplate().queryForList(sql);
	}
	/**
	 * 更新用户
	 * @param pids
	 * @return
	 */
	@Override
	public void updateUser(User user) {
		userDao.update(user);
	}

	/**
	 * 管理员删除价格
	 * @param pids
	 * @return
	 */
	@Override
	public void deletePriceById(Integer id) {
		String sql = " delete from tab_price where id=?";
		mtJdbcDao.getJdbcTemplate().update(sql, new Object[]{id});
		
	}
	
	/**
	 * 管理员修改价格
	 * @param pids
	 * @return
	 */
	@Override
	public void updatePrice(Integer id, String num, Double price,Integer time) {
		String sql = " update tab_price set num=?, price =?,time=? where id=?";
		mtJdbcDao.getJdbcTemplate().update(sql,new Object[]{num,price,time,id});
	}
	/**
	 * 新增价格
	 * @param pids
	 * @return
	 */
	public void addPrice(Integer num,Double price,Integer type) {
		Integer time = 0;
		if(type == 2) {
			time = 24;
		} else {
			time = 30;
		}
		String sql = " insert into tab_price(num,price,type,time) values (?,?,?,?)";
		mtJdbcDao.getJdbcTemplate().update(sql, new Object[]{num,price,type,time});
	}

	public MtJdbcDao getMtJdbcDao() {
		return mtJdbcDao;
	}

	public void setMtJdbcDao(MtJdbcDao mtJdbcDao) {
		this.mtJdbcDao = mtJdbcDao;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public RecordDao getRecordDao() {
		return recordDao;
	}
	public void setRecordDao(RecordDao recordDao) {
		this.recordDao = recordDao;
	}

}
